//
//  BVCategoryTool.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/12.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVCategoryTool.h"
#import "BVParentList.h"
#import "BVGoodsList.h"

@implementation BVCategoryTool

- (void)fetchParentListSuccess:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/GoodsAPI/c_get_parenttype",PATH];
    [BVHttpRequest getWithUrlString:url parameters:nil success:^(id responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"0"]) {
            NSArray *parentArr = [BVParentList mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];
            success(parentArr);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)fetchChildListByParentID:(NSString *)parentId success:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/GoodsAPI/c_get_childtype",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"parent_id"] = parentId;
    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"0"]) {
            NSArray *childArr = [BVChildList mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];
            success(childArr);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)fetchGoodsListByChildID:(NSString *)childId type:(NSString *)type cityID:(NSString *)cityId success:(void (^)(BVGoodsList *))success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/GoodsAPI/c_get_goodslist",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"child_id"] = childId;
    params[@"type"] = type;
    params[@"city_id"] = cityId;
    params[@"user_id"] = self.user.login ? self.user.user_id:@"";
//    params
    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        BVGoodsList *goodList = [BVGoodsList mj_objectWithKeyValues:responseObject[@"result"]];
        NSLog(@"goods_num:%@",goodList.goods_num);
        success(goodList);
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)addGoodsToShoppingCart:(NSString *)goodsId success:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/GoodsAPI/c_add_shopcart",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id"] = goodsId;
    params[@"user_id"] = self.user.user_id;

    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"0"]) {
            if (success) {
                success();
            }
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

}


@end
